﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LSI_interface
{
    public partial class Form1 : Form
    {
        Compute oCompute;

        public Form1()
        {
            InitializeComponent();
            oCompute = new Compute();
        }

        private void btnRetrieveDB_Click(object sender, EventArgs e)
        {
            txtMsg.Text = "Creating TFM...";

            oCompute.createTFM();

            btnLaunch.Enabled = true;
            btnImport.Enabled = true;

            txtMsg.Text = "Creation finished.";
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            txtMsg.Text = "Computing SVD...";

            oCompute.pseudoTermNum = (int)numericUpDown1.Value;

            oCompute.ComputeSVD();

            cboGroup.Items.Clear();
            foreach (int i in oCompute.Groups)
            {
                cboGroup.Items.Add(i.ToString());
            }

            txtMsg.Text = "Approxiate error: " + oCompute.appError.ToString();

            btnExport.Enabled = true;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPath.Text = dialog.SelectedPath;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            StringBuilder textBuild = new StringBuilder();
            textBuild.AppendLine(oCompute.appError.ToString());

            foreach (int line in oCompute.Groups)
            {
                textBuild.AppendLine(line.ToString());
            }

            StreamWriter file = new StreamWriter(txtPath.Text + "\\\\Groups.txt");
            file.WriteLine(textBuild);

            file.Close();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            List<string> titlesGroup = oCompute.RetrieveGroup(int.Parse(cboGroup.Text));

            dataGridView1.Rows.Clear();
            for (int i = 0; i < titlesGroup.Count; i++)
            {
                dataGridView1.Rows.Add((i + 1).ToString(), titlesGroup[i]);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fd.FileName))
                    {
                        oCompute.appError = double.Parse( sr.ReadLine() );
                        txtMsg.Text = "Approximate error: " + oCompute.appError.ToString();

                        List<string> lines = new List<string>();

                        while (!sr.EndOfStream)
                        {
                            lines.Add(sr.ReadLine());
                        }
                        oCompute.Groups = new int[lines.Count - 1];
                        for (int i = 0; i < lines.Count - 1; i++)
                            oCompute.Groups[i] = int.Parse(lines[i]);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(ex.Message);
                }
            }

            cboGroup.Items.Clear();
            foreach (int i in oCompute.Groups)
            {
                cboGroup.Items.Add(i.ToString());
            }
        }

        private void btnRetrieveDB_Click_1(object sender, EventArgs e)
        {
            oCompute.RetrieveDB();

            btnCreateTFM.Enabled = true;
        }
    }
}
